/*
 * Created: 2022-08-31 08:59:04
 * Author : 涛涛涛
 */

import { useEffect, useState } from 'react';
import { figureTime_hhmmss } from '@/utils';
const localFigureDownTime = (dateStr: string) => {
    if (new Date(dateStr).getTime() < Date.now()) return '已结束';
    const { hour, minute, second } = figureTime_hhmmss(new Date(dateStr).getTime() - Date.now());
    return hour + ':' + minute + ':' + second;
};

/** 使用倒计时的时间 */
export const useDownTime = (dateStr: string) => {
    const [downTime, setDownTime] = useState(() => localFigureDownTime(dateStr));
    useEffect(() => {
        const intervalId = setInterval(() => {
            setDownTime(localFigureDownTime(dateStr));
        }, 1000);
        return () => {
            clearTimeout(intervalId);
        };
    }, [dateStr]);
    return {
        downTime,
    };
};
